# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright © 2018-2019 Ariadne Devos
# sHT -- build system -- list of dependencies
AUTOMAKE_OPTIONS = subdir-objects foreign no-dist \
 -Wobsolete -Woverride -Wunsupported -Werror

AM_CPPFLAGS = -D_GNU_SOURCE "-I$(srcdir)/arch/$(CONFIG_ARCH)" -Og -g2

bin_PROGRAMS = shttpd
shtsources = \
  buffer/append.c \
  buffer/lex.c \
  buffer/memeq.c \
  buffer/skip.c \
  fd/fd.c \
  fd/inet.c \
  generic/bug.c \
  lex/ipv4.c \
  lex/nat.c \
  task/accept.c \
  task/perform.c \
  task/sockrw.c \
  task/stream.c \
  worker/alloc.c \
  worker/block-mmap.c \
  worker/edge-epoll.c \
  worker/loop.c \
  worker/objcache.c \
  worker/schedule.c \
  worker/vector.c

worker/objcache.c: worker/objcache-bless-table.c
	touch -c $@
worker/objcache-bless-table.c: tools/gen-vector-variants.sh
	$< size sHT_objcache_bless objcache-bless.c > $@T && mv -T $@T $@

tests/%$(EXEEXT): tests/%.o $(shttpd_OBJECTS) $(shttpd_DEPENDENCIES) $(EXTRA_shttpd_DEPENDENCIES)
	@rm -f shttpd$(EXEEXT)
	$(AM_V_CCLD)$(LINK) $< $(shttpd_OBJECTS) $(shttpd_LDADD) $(LIBS)

tests = \
	tests/append \
	tests/cmp \
	tests/lex \
	tests/ipv4 \
	tests/memeq \
	tests/index-nospec

testresult = tests/append
%.result: %
	./$< > "$@" 2>&1; \
	cat "$@" || exit 1; \
	if [ "$$?" -eq 0 ]; then \
		rm -f "$@-fail" ; \
		touch "$@-pass" || exit 1; \
	else \
		rm -f "$@-pass" ; \
		touch "$@-fail" || exit 1; \
	fi

# TODO: fatal error: opening dependency file tests/.deps/append.Tpo
# Work-around: mkdir tests/.deps
# Future solution:
# "don't write a portable Makefile, use a portable (GNU) make instead!"
# (Mad Scientist's general advice)
# don't support all platforms (s^2 requires architecture-specific support,
# and WILL be formally verified for array bounds.
test: $(foreach t,$(tests),$(t).result)
	@ echo Summary
	@ cat $(foreach t,$(tests),$(t).result)
	@ cat $(foreach t,$(tests),$(t).result-pass) || (echo 'not all tests passed' && exit 1)

shttpdsources = \
  http/accept.c \
  http/stream.c

shttpd_SOURCES = $(shtsources) $(shttpdsources)

# Build the documentation with Sphinx.
# It is configured in $(srcdir)/doc/conf.py.
# <http://sphinx-doc.org/stable/>.
#
# -n: warn about all missing references
# -q: only output warning
# -W: turn warnings into errors
# -d: cache directory
# sourcedir, outdir
html-local:
	sphinx-build -b html -n -q -d $(builddir)/doctrees $(srcdir)/doc $(builddir)/html
mostlyclean-local:
	-rm -rf $(builddir)/html $(builddir)/doctrees
# TODO: install the documentation
